package msg

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"
	//"github.com/wailsapp/wails/v2/pkg/runtime"
	//"context"
)

var LogAble bool = false

var MsgChan = make(chan string) //通道
var MsgMutex sync.Mutex

var MsgtimeoutChan = make(chan string) //通道
var MsgtimeoutChanMutex sync.Mutex

var NatCheckChan = make(chan string) //通道
var NatCheckChanMutex sync.Mutex

var ErrChan = make(chan string) //通道
var errlock sync.Mutex

var PanicChan = make(chan string) //通道
var Paniclock sync.Mutex

var SuccessChan = make(chan string) //通道
var successlock sync.Mutex

var ConfigChan = make(chan string) //通道
var configlock sync.Mutex

func SendMessage(format string, args ...interface{}) {
	MsgMutex.Lock()
	defer MsgMutex.Unlock()

	if LogAble {
		Logtofile(fmt.Sprintf("%s", format))
	}
	MsgChan <- fmt.Sprintf(format, args...)
}

func ConfigCheck(format string, args ...interface{}) {
	configlock.Lock()
	defer configlock.Unlock()
	// println(fmt.Printf(format, args...))
	ConfigChan <- fmt.Sprintf(format, args...)
}

// 连接超时发送消息
func SendTimeoutMessage(format string, args ...interface{}) {
	MsgtimeoutChanMutex.Lock()
	defer MsgtimeoutChanMutex.Unlock()
	// fmt.Printf(format, args...)

	if LogAble {
		Logtofile(fmt.Sprintf("%s", format))
	}
	MsgtimeoutChan <- fmt.Sprintf(format, args...)
}

// 发送nat消息
func NatCheck(format string, args ...interface{}) {
	NatCheckChanMutex.Lock()
	defer NatCheckChanMutex.Unlock()
	// fmt.Printf(format, args...)
	if LogAble {
		Logtofile(fmt.Sprintf("%s", format))
	}
	NatCheckChan <- fmt.Sprintf(format, args...)
}

// 发送错误信息消息
func ErrCheck(format string, args ...interface{}) {
	errlock.Lock()
	defer errlock.Unlock()
	if LogAble {
		ErrorLog(fmt.Sprintf("%s", format))
	}
	ErrChan <- fmt.Sprintf(format, args...)
}

// 程序崩溃信息消息
func PanicCheck(format string, args ...interface{}) {
	errlock.Lock()
	defer errlock.Unlock()
	if LogAble {
		PanicLog(fmt.Sprintf("%s", format))
	}
	ErrChan <- fmt.Sprintf(format, args...)
}

// 发送操作成功信息消息
func SuccessCheck(format string, args ...interface{}) {
	successlock.Lock()
	defer successlock.Unlock()
	Logtofile(fmt.Sprintf("%s", format))
	SuccessChan <- fmt.Sprintf(format, args...)
}

func RotateLogFile(logFilePath string) (*log.Logger, error) {
	now := time.Now()
	logFileName := now.Format("2006-01-02") + ".log"
	fullPath := filepath.Join(logFilePath, logFileName)

	// 确保日志文件的目录存在
	if err := os.MkdirAll(logFilePath, 0755); err != nil {
		return nil, err
	}

	// 创建或打开日志文件
	file, err := os.OpenFile(fullPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		return nil, err
	}
	return log.New(file, "", log.LstdFlags), nil
}
func Logtofile(logtext string) {
	logDir := "./logs/log"
	logger, err := RotateLogFile(logDir)
	if err != nil {
		log.Fatalf("Failed to create logger: %v", err)
	}
	// 使用logger记录日志
	logger.Println(logtext)
}

// 崩溃原因记录日志
func PanicLog(logtext string) {
	logDir := "./logs/panic"
	logger, err := RotateLogFile(logDir)
	if err != nil {
		log.Fatalf("Failed to create logger: %v", err)
	}

	// 使用logger记录日志
	logger.Println(logtext)
}

// 错误记录日志
func ErrorLog(logtext string) {
	logDir := "./logs/err"
	logger, err := RotateLogFile(logDir)
	if err != nil {
		log.Fatalf("Failed to create logger: %v", err)
	}

	// 使用logger记录日志
	logger.Println(logtext)
}
